/**
/*
 * 编辑机械设备
 * @Author: zfx
 * @Date: 2018-07-02 15:53:05
 */
<template>
	<div class="v-admin-sell-component">
		<Form
			ref="formValidate"
			:model="formValidate"
			:rules="ruleValidate"
			:label-width="labelWidth(70,80)"
			name="sell-edit-form">

			<Form-item
				:label="$t('equipment.device_spec')"
				prop="spec_id">
				<Select
					v-model="formValidate.spec_id"
					name="sell-spec_id"
					clearable
					filterable
					transfer>
					<Option
						v-for="( value , key ) in deviceSpecs"
						:value="value.id + ''"
						:key="key">{{ value.code }}</Option>
				</Select>
			</Form-item>

			<Form-item>
				<Button
					type="primary"
					@click="handleSubmit('formValidate')">
					{{ $t('common.update') }}
				</Button>
			</Form-item>

		</Form>
	</div>
</template>
<script>

import labelWidthMixin from 'src/views/mixins/labelWidthMixin';

export default {

	name: 'MachineEdit',
	mixins: [labelWidthMixin],
	props: {
		machineDetail: {
			type: Object,
			default() {
				return {};
			},
		},
		showModal: {
			type: Boolean,
			default() {
				return false;
			},
		},
		deviceSpecs: {
			type: Array,
			default() {
				return [];
			},
		},
	},

	data() {
		return {
			formValidate: {

				spec_id: '',

				items: [
					{
						value: '',
					},
				],
			},

			ruleValidate: { // 表单验证规则
				spec_id: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('equipment.device_spec') }),
						trigger: 'change',
					},

				],
			},

		};
	},

	watch: {
		machineDetail() {
			if (this.machineDetail) {
				this.formValidate.spec_id = String(this.machineDetail.device_spec_id);
			}
		},
		showModal() {
			if (!this.showModal) {
				this.handleReset('formValidate');
			}
		},
	},

	methods: {

		/**
         * 提交表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleSubmit(name) {
			this.$refs[name].validate((valid) => {
				if (valid) {
					this.$emit('post', this.machineDetail.id, this.handleData());
				} else {
					this.$Message.error(this.$t('common.exist_empty_field'));
				}
			});
		},

		/**
         * 重置表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleReset(name) {
			this.$refs[name].resetFields();
		},

		/**
         * 手动拼接表单数据
		 * @param {Object} new_object {表单对象}
         * @return {undefined}
         */
		handleData() {
			const new_object = {

				_method: 'PUT',

				device_spec_id: this.formValidate.spec_id,

			};
			return new_object;
		},
	},
};
</script>
